package com.xianniu.ltc.api;

import com.alibaba.fastjson.JSON;
import com.rebuild.core.UserContextHolder;
import com.xianniu.ltc.model.po.*;
import com.xianniu.ltc.model.vo.RobotApprovalStepVO;
import com.xianniu.ltc.service.ExternalService;
import com.xxl.sso.core.entity.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 接收携带Token的外部接口：报价单
 * @author WangRuiJie
 * @Title: ApiController
 * @Package com.xianniu.ltc.api
 * @Description:
 * @date 2021/11/11:58 下午
 */
@Slf4j
@RestController
@RequestMapping(value = "/apiController")
public class ApiController {

    @Autowired
    private ExternalService externalService;

    /***************************************** 报价系统相关接口 开始 *********************************************/
    /**
     * 采购云发布询价新增报价协同数据
     * @param request
     * @return
     */
    @PostMapping("/updateQuotationToLtc")
    public ReturnT updateQuotationToLtc(@RequestBody UpdateQuotePO po, HttpServletRequest request){
        if (StringUtils.isEmpty(po.getLtcId())){
            log.info("报价单ID不能为空");
            return new ReturnT(400, "报价单ID不能为空");
        }
        log.info("报价系统通过ID修改：{}", JSON.toJSON(po));
        return externalService.updateQuotationToLtc(po);
    }
    /***************************************** 报价系统相关接口 结束 *********************************************/

    /***************************************** APP我的审批接口 开始 *********************************************/
    /**
     * APP新需求-查询消息/待办的审批
     * @param po
     * @param request
     * @return
     */
    @PostMapping("/apiApprovalList")
    public ReturnT apiApprovalList(@RequestBody ApprovalListQueryPO po, HttpServletRequest request) {
       log.info("APP新需求-查询消息/待办的审批:{}", JSON.toJSON(po));
       return externalService.apiApprovalList(po);

    }

    /**
     * APP新需求-查询抄送的审批数据
     * @param relatedRecords
     * @param request
     * @return
     */
    @PostMapping("/apiCcApprovalList")
    public ReturnT apiCcApprovalList(@RequestBody List<RobotApprovalStepVO> relatedRecords, HttpServletRequest request) {
        log.info("APP新需求-查询抄送的审批参数:{}", JSON.toJSON(relatedRecords));
        return externalService.apiCcApprovalList(relatedRecords);

    }

    /**
     * APP-查询企业下销售云用户
     * @param request
     * @return
     */
    @PostMapping("/selectLtcUserList")
    public ReturnT selectLtcUserList(@RequestParam String param, HttpServletRequest request){
        log.info("APP-查询企业下销售云用户：{}", param);
        return externalService.selectLtcUserList(param);
    }

    /***************************************** APP我的审批接口 结束 *********************************************/

    /***************************************** 接收采购订单接口 开始 *********************************************/

    /**
     * 接收采购云的采购订单
     * @param request
     * @return
     */
    @PostMapping("/receivePmsPurchaseOrder")
    public ReturnT receivePmsPurchaseOrder(@RequestBody PmsPurchaseOrderPO po, HttpServletRequest request){
        log.info("接收采购云的采购订单：{}", JSON.toJSON(po));
        return externalService.receivePmsPurchaseOrder(po);
    }

    /**
     * 修改采购云的采购订单状态，补充备件对应SN，物流公司，物流单号
     * @param request
     * @return
     */
    @PostMapping("/updatePmsPurchaseOrder")
    public ReturnT updatePmsPurchaseOrder(@RequestBody PmsUpdatePurchaseOrderPO po, HttpServletRequest request){
        log.info("修改采购云的采购订单状态，补充备件对应SN，物流公司，物流单号信息：{}", JSON.toJSON(po));
        if (po.getTenantCode() == null){
            return new ReturnT(500, "采购方的租户编码不能为空");
        }
        UserContextHolder.setTenant(po.getTenantCode());
        if (po.getCaigoudingdanhao() == null){
            return new ReturnT(500, "采购订单不能为空");
        }
        return externalService.updatePmsPurchaseOrder(po);
    }

    /***************************************** 接收采购订单接口 结束 *********************************************/

}
